package cheme.ProcessModel.Visitors;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectiveVisitor implements IVisitor
{
	public Object visit(Object subject)
	{
		Object result = null;
		
		try
		{
			Method method = getClass().getMethod("visit", new Class[] { subject.getClass() });

			if (null != method)
			{
				result = (String)method.invoke(this, new Object[] {subject});
			}
			else
			{
				defaultVisit(subject);
			}
		}
		catch (NoSuchMethodException e)
		{
			result = this.defaultVisit(subject);
		}
		catch (InvocationTargetException e)
		{
			result = this.defaultVisit(subject);
		}   
		catch (IllegalAccessException e)
		{
			result = this.defaultVisit(subject);
		}  
		
		return result;
	}
	
	public Object defaultVisit(Object visitable)
	{
		return visitable.toString();
	}
}